﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Models
{
    public class ContractList
    {
        public PageInfo Init()
        {
            PageInfo pg = new PageInfo();
            pg.Type = "Contract";
            pg.Title = "合同中心";
            pg.AddText = "新增合同";
            pg.EditText = "修改";
            pg.LocateText = "查询";
            pg.SMenuId = "HT01";
            pg.FileUrl = "../Contract/ContractList";
            pg.Condition = "";
            pg.TableName = "cu_Contract";
            pg.KeyFieldName = "id";
            pg.PopEditHeight = 650;
            pg.PopEditWidth = 860;
            HttpSessionState session = System.Web.HttpContext.Current.Session;
            pg.dict = BasePage.getDict(session);
            pg.Sop = BasePage.Grant(session, pg.SMenuId);
           

            pg.Sql = @"select cu_Contract.id, custname ,LinkName, cu_Customers.CustTel,contractNumber,case when (select IsClassHour from cu_sys_contract where cu_sys_contract.id=Setmealid)=0 then convert(varchar(10),ConEnd,120) else convert(varchar(10),SurplusNumber) end Surplus ,
            convert(varchar(10),ContractPrice)+'/'+convert(varchar(10),DisPrice) Price,convert(varchar(10),constart,120)+'-'+convert(varchar(10),conend,120) ConSEnd,AchievementUser.sshowname AchievName,
             case status when 0 then  '未开始执行' when 1 then '执行中' when 2 then '延期中' when 3 then '作废（退费）' end statusName,
             (select top 1 dname from tb_sys_dictionary  where did=method) method,
            CardNumber,contracttype,SetmealId,SetmealName,ContractPrice,DisPrice,cu_Contract.ContractDate,Constart,ConEnd,Remark,MaxLeave,MaxMakeUP,ConCourseNumber,GiveCourseNumber,isnull(Gift1,'') Gift1,isnull(Gift1Price,0) Gift1Price,isnull(Gift2,'') Gift2,isnull(Gift2Price,0) Gif2Price,isnull(Gift3,'') Gift3,isnull(Gift3Price,0) Gift3Price,state,status,IsCharge,cu_Contract.CreateId,cu_Contract.CreateDate,AuditingId,AuditingDate,AuditingRemark,AchievementId,cu_Contract.LeagueId from cu_Contract
             left join ( select CustId,contractId,COUNT(*) sl from cu_Contract_Customer group by CustId,contractId)   cu_Contract_Customer on cu_Contract_Customer.contractId = cu_Contract.id  
            left join cu_Customers on cu_Contract_Customer.CustId = cu_Customers.id
              left join tb_sys_users AchievementUser on AchievementUser.iuserid = cu_Contract.AchievementId
            where cu_Contract.LeagueId = 1";
            
            ColumnInfo cl;
            cl = new ColumnInfo(); cl.FieldName = "contractNumber"; cl.HeadText = "合同编号"; cl.Visible = true; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "custname"; cl.HeadText = "学员姓名"; cl.Visible = true; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "CustTel"; cl.HeadText = "联系电话"; cl.Visible = true; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "SetmealName"; cl.HeadText = "合同套餐"; cl.Visible = true; cl.DisplayFormat = "{0:dd / MM / yyyy}"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "Surplus"; cl.HeadText = "课程剩余"; cl.Visible = true; cl.IsSum = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "Price"; cl.HeadText = "应收/实收金额"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "GiveCourseNumber"; cl.HeadText = "赠送课时"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "ContractDate"; cl.HeadText = "签约日期"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "ConSEnd"; cl.HeadText = "合同有效期"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "AchievName"; cl.HeadText = "业绩所属人"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "contracttype"; cl.HeadText = "合同类型"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "statusName"; cl.HeadText = "合同状态"; cl.Visible = true; pg.AddColumn(cl);
            
            return pg;
        }
    }
}